package cn.javass.struts2impl.hello.action;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.javass.struts2impl.hello.model.HelloWorldModel;
import cn.javass.struts2impl.preResultListener.MyPreResult;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction2 extends ActionSupport implements SessionAware {
    /**
     * 
     */
    private static final long serialVersionUID = -1510386396110428941L;

    private HelloWorldModel hwm;
    
    private Map<String, Object> session;

    public String execute() throws Exception {
        // 1:收集参数
        // 2:组织参数
        // 3:调用模型的逻辑功能处理
        System.out.println("用户输入的参数为====" + "account=" + hwm.getAccount() + ",passsword=" + hwm.getPassword() + ",submitFlag=" + hwm.getSubmitFlag());
        // 实现SessionAware接口，使用对象Map<String, Object> session注入Session数据
        session.put("login_user.account", hwm.getAccount());
        // 4:根据逻辑处理的结果来选择下一个页面
        return "toWelcome";
    }
    
    public void validate() {
        String account = hwm.getAccount();
        String password = hwm.getPassword();
        String submitFlag = hwm.getSubmitFlag();
        
        if(account==null || account.trim().length()==0){
            this.addFieldError("account", "账号不允许为空");
        }
        if(password==null || password.trim().length()==0){
            this.addFieldError("password", "密码不允许为空");
        }
        if(password==null || password.trim().length()<6){
            this.addFieldError("password", "密码长度必须在6位以上");
        }
    }
    
    public HelloWorldModel getHwm() {
        return hwm;
    }

    public void setHwm(HelloWorldModel hwm) {
        this.hwm = hwm;
    }

    public Map<String, Object> getSession() {
        return session;
    }

    public void setSession(Map<String, Object> session) {
        this.session = session;
    }
    
}
